package com.superman.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.superman.domain.User;
import com.superman.service.UserService;
import com.superman.serviceImpl.UserServiceImpl;

public class RegisterServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取验证码，检验是否正确
		String in_checkcode = request.getParameter("checkcode");
		String real_checkcode = (String) request.getSession().getAttribute("checkcode");
		if (!real_checkcode.equalsIgnoreCase(in_checkcode)) {
			request.setAttribute("msg", "亲，您的验证码有误，请重新输入");
			request.getRequestDispatcher("/register.jsp").forward(request, response);
		} else {
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String nickname = request.getParameter("nickname");
			User user = new User();
			user.setEmail(email);
			user.setPassword(password);
			user.setNickname(nickname);
			UserService service = new UserServiceImpl();
			int register = service.register(user);
			if (register != 1) {
				request.setAttribute("msg", "注册失败");
				request.getRequestDispatcher("/register.jsp").forward(request, response);
			} else {
				response.sendRedirect(request.getContextPath() + "/login.jsp");
			}
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}